package com.sun.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sun.common.Result;
import com.sun.common.SystemCrmlog;
import com.sun.entity.ReturnGoods;
import com.sun.service.ReturnGoodsService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

/**
 * @Project: hospitalDrugInformationManagement
 * @Author: 86178
 * @Date: 2023/4/3 12:26
 * @FileName: ReturnGoodsController
 * @Description: ""
 */
@RestController
@RequestMapping("/returnGoods")
public class ReturnGoodsController {

    @Autowired
    private ReturnGoodsService returnGoodsService;

    /**
     * 分页查询退货数据
     *
     * @param page
     * @param pageSize
     * @param drugName
     * @return
     */
    @GetMapping("/page")
    public Result<Page> page(@RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
                             @RequestParam(value = "limit", defaultValue = "10") int pageSize,
                             String drugName) {

        Page<ReturnGoods> pageInfo = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<ReturnGoods> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(StringUtils.isNotEmpty(drugName), ReturnGoods::getDrugName, drugName);
        returnGoodsService.page(pageInfo, lambdaQueryWrapper);
        return Result.success(pageInfo);
    }


    /**
     * 添加一个退货数据
     */
    @SystemCrmlog(title = "销售管理模块", type = "INSERT", description = "添加一个退货数据")
    @PostMapping
    public Result<String> add(ReturnGoods returnGoods) {
        returnGoods.setOperateTime(LocalDateTime.now());
        returnGoodsService.save(returnGoods);
        return Result.success("新增成功");
    }

    /**
     * 删除一个退货数据
     */
    @SystemCrmlog(title = "销售管理模块", type = "DELETE", description = "删除一个退货数据")
    @DeleteMapping
    public Object delete(Long id) {
        returnGoodsService.removeById(id);
        return Result.success("删除成功");
    }


    /**
     * 修改一个退货数据
     */
    @SystemCrmlog(title = "销售管理模块", type = "UPDATE", description = "修改一个退货数据")
    @PutMapping
    public Object update(ReturnGoods returnGoods) {
        returnGoodsService.updateById(returnGoods);
        return Result.success("修改成功");
    }
}
